using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace MicroMsg.Storage
{
	public class StorageIntKey<T> : StorageMemory<T, int> where T : StorageItem
	{
		private int maxKeyID = -1;
		private Func<T, int> getKey;
		private Func<T, int, int> setKey;
		public StorageIntKey(Expression<Func<T, int>> func_getKeyExpr, Func<T, int, int> func_setKey) : base(func_getKeyExpr)
		{
			this.getKey = func_getKeyExpr.Compile();
			this.setKey = func_setKey;
		}
		protected override bool init()
		{
			if (!base.init())
			{
				return false;
			}
			List<T> list = base.getList();
			if (list != null && list.get_Count() > 0)
			{
				this.maxKeyID = Enumerable.Max<T>(list, this.getKey);
				return true;
			}
			this.maxKeyID = 0;
			return true;
		}
		protected override void uninit()
		{
			this.maxKeyID = -1;
			base.uninit();
		}
		public override bool vmethod_0(T item)
		{
			if (item != null && this.getKey.Invoke(item) == 0 && base.isReady())
			{
				this.setKey.Invoke(item, ++this.maxKeyID);
				return base.vmethod_0(item);
			}
			return false;
		}
	}
}
